/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package download.manga.hilos;

import download.manga.DownloadGui;
import java.io.*;
import java.util.Properties;
import javax.swing.JComboBox;
import utils.servers.SubManga;

/**
 *
 * @author killan
 */
public class HiloDescarga extends Thread {

    private boolean continuar = true;
    private String action = "", globPath = "";
    private SubManga sm = new SubManga();
    private Object parent;
    private File f, f2;
    private Properties prop = new Properties();

    public void detenerHilo() {
        continuar = false;
    }

    @Override
    public void run() {
        if (globPath.equals("")) {
            f = new File("./propi/pathProperties.properties");
            try {
                prop.load(new FileInputStream(f));
            } catch (Exception e) {
                System.out.println(e);
            }

            globPath = prop.getProperty("globPath");
        }
        while (continuar) {
            if (action.equals("getList")) {
                this.synchGetMList();
            } else if (action.equalsIgnoreCase("getChap")) {
                this.synchGetCList();
            } else if (action.equalsIgnoreCase("getNPag")) {
                this.syncGetNPages();
            } else if (action.equalsIgnoreCase("getPag")) {
                this.syncGetPages();
            } else if (action.equalsIgnoreCase("getMan")) {
                this.syncGetManga();
            }
        }
    }

    public void getMangaList(DownloadGui dg) {
        this.parent = dg;
        action = "getList";

        if (!this.isAlive()) {
            this.start();
        }
        if (!continuar) {
            continuar = true;
        }
    }

    public void getManga(DownloadGui dg) {
        this.parent = dg;
        action = "getMan";

        if (!this.isAlive()) {
            this.start();
        }
        if (!continuar) {
            continuar = true;
        }
    }

    public void getChapterList(DownloadGui dg) {
        this.parent = dg;
        action = "getChap";

        if (!this.isAlive()) {
            this.start();
        }
        if (!continuar) {
            continuar = true;
        }
    }

    public void getNumberPages(DownloadGui dg) {
        this.parent = dg;
        action = "getNPag";

        if (!this.isAlive()) {
            this.start();
        }
        if (!continuar) {
            continuar = true;
        }
    }

    public void getPages(DownloadGui dg) {
        this.parent = dg;
        action = "getPag";

        if (!this.isAlive()) {
            this.start();
        }
        if (!continuar) {
            continuar = true;
        }
    }

    private synchronized void synchGetMList() {
        sm = new SubManga();
        sm.getSeries(globPath);
        try {
            f = new File(globPath + "Conf/series.konf");
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            if ((linea = br.readLine()) != null) {
                sm.getChaptersList(globPath, linea, "completa");
            }
            br.close();

        } catch (Exception ex) {
        }
        if (parent instanceof DownloadGui) {
            DownloadGui dg = (DownloadGui) parent;
            dg.setJC1();
        }
        action = "";
    }

    private synchronized void synchGetCList() {
        sm = new SubManga();

        if (parent instanceof DownloadGui) {
            DownloadGui dg = (DownloadGui) parent;
            f = new File(globPath + "Conf/MangaList/" + dg.getJCB1() + ".konf");
            f2 = new File(globPath + "Conf/MangaList/" + dg.getJCB1() + ".txt");
            if ((!f.exists()) && (!f2.exists())) {
                JComboBox jc = dg.getAJCB2();
                jc.removeAllItems();
                jc.addItem("Wait...");

                if (dg.getJCB1().equals("To_Love_Ru")) {
                    sm.getChaptersList(globPath, dg.getJCB1(), "scanlation/Sukebe_Buntai");
                } else {
                    sm.getChaptersList(globPath, dg.getJCB1(), "completa");
                }
            }
            dg.setJC2();
        }
        action = "";
        syncGetNPages();
    }

    private synchronized void syncGetNPages() {
        if (parent instanceof DownloadGui) {
            DownloadGui dg = (DownloadGui) parent;
            String mang = dg.getJCB1();
            String cap = dg.getJCB2();
            dg.setJLabel3("Paginas " + sm.getNumPages(mang, cap));
        }
        action = "";
    }

    private synchronized void syncGetManga() {
        if (parent instanceof DownloadGui) {
            DownloadGui dg = (DownloadGui) parent;
            String mang = dg.getJCB1();
            //String cap = dg.getJCB2();
            int idx = dg.getJCB2Count();
            JComboBox jc = dg.getAJCB2();
            for (int i = idx - 1; i >= 0; i--) {
                String cap = jc.getItemAt(i).toString();
                dg.setJLabel3("Paginas: 0/" + sm.getNumPages(mang, cap));
                dg.setCapLabel("Capitulo " + cap);
                while (!sm.getPages(mang, cap, dg)) {
                }
            }
        }
        action = "";
    }

    private synchronized void syncGetPages() {
        if (parent instanceof DownloadGui) {
            DownloadGui dg = (DownloadGui) parent;
            String mang = dg.getJCB1();
            String cap = dg.getJCB2();
            dg.setJLabel3("Paginas: 0/" + sm.getNumPages(mang, cap));
            sm.getPages(mang, cap, dg);

        }
        action = "";
    }
}
